'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, BOOLEAN, DATE, DECIMAL, ENUM } = app.Sequelize;

  const Recommendation = app.model.define('recommendation', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    type: {
      type: ENUM('weather', 'occasion', 'color', 'personal', 'similar_user', 'smart_outfit', 'daily'),
      allowNull: false,
      comment: '推荐类型',
    },
    title: {
      type: STRING(200),
      allowNull: false,
      comment: '推荐标题',
    },
    description: {
      type: TEXT,
      comment: '推荐描述',
    },
    outfit_ids: {
      type: TEXT,
      comment: '推荐的搭配ID列表(JSON)',
    },
    clothing_ids: {
      type: TEXT,
      comment: '推荐的衣服ID列表(JSON)',
    },
    context: {
      type: TEXT,
      comment: '推荐上下文信息(JSON)',
    },
    confidence_score: {
      type: DECIMAL(3, 2),
      defaultValue: 0.5,
      comment: '推荐置信度(0-1)',
    },
    is_accepted: {
      type: BOOLEAN,
      defaultValue: null,
      comment: '用户是否接受推荐',
    },
    feedback_rating: {
      type: INTEGER,
      comment: '用户反馈评分(1-5)',
    },
    feedback_comment: {
      type: TEXT,
      comment: '用户反馈评论',
    },
    expires_at: {
      type: DATE,
      comment: '推荐过期时间',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'recommendations',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '推荐记录表',
  });

  // 关联关系
  Recommendation.associate = function() {
    // 属于用户
    app.model.Recommendation.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });
  };

  return Recommendation;
};
